package com.bl.weilan.service.biz.information.impl;
import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.dao.model.InformationEntity;
import com.bl.weilan.service.biz.information.InformationService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.information.InformationAddVo;
import com.bl.weilan.service.vo.information.InformationListVo;
import com.bl.weilan.service.vo.information.InformationModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.bl.weilan.dao.repository.InformationRepository;
import com.bl.weilan.common.utils.UUIDUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class InformationServiceImpl implements InformationService {
    @Autowired
    InformationRepository informationRepository;


    @Override
    public InformationListVo getInformationList(Pageable pageable){
        Page <InformationEntity> informationEntities;
         InformationListVo informationListVo = new InformationListVo();

        informationEntities = informationRepository.findByOrderByWeightDesc(pageable);

        informationListVo.setTotalCount(informationEntities.getTotalElements());
        informationListVo.setList(informationEntities.getContent());

        return informationListVo;
    }

    @Override
    public InformationEntity getInformationById(String id){
        InformationEntity informationEntity = new InformationEntity();
        informationEntity = informationRepository.findOne(id);

        return informationEntity;
    }

    @Override
    public void addInformation(InformationAddVo informationAddVo) {

        InformationEntity informationEntity = new InformationEntity();

        informationEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(informationAddVo, informationEntity);

        informationRepository.save(informationEntity);
    }

    @Override
    public void del(DelBody id) {
        informationRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            informationRepository.delete(id);
        }
    }

    @Override
    public void modify(InformationModVo informationModVo){
        InformationEntity informationEntity = informationRepository.findOne(informationModVo.getId());

        if (informationEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(informationModVo, informationEntity);
        informationRepository.save(informationEntity);
    }


}
